﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Plugins;

using Hetymine.Executioner.Data;
using Hetymine.Executioner.Plugins;

namespace Hetymine.Executioner.Controls {
    /// <summary>
    /// Interaction logic for PluginConfigurator.xaml
    /// </summary>
    public partial class PluginConfigurator : UserControl, IConfigurator<List<PluginInstance<Plugin>>> {
        public PluginConfigurator() {
            InitializeComponent();
        }

        #region IConfigurator<List<PluginInstance<Plugin>>> Members
        public void InitializeConfig(List<PluginInstance<Plugin>> plugins) {
            this.cPlugins.Children.Clear();
            foreach (PluginInstance<Plugin> p in plugins) {
                Expander e = new Expander();
                if (this.cPlugins.Children.Count == 0) {
                    e.Margin = new Thickness(0);
                }
                else {
                    e.Margin = new Thickness(0, 10, 0, 0);
                }

                if (!p.Instance.PluginInfo.Name.IsEmpty()) {
                    e.Header = p.Instance.PluginInfo.Name;
                }
                else {
                    e.Header = "<unknown>";
                }
                e.Expanded += new RoutedEventHandler(Expanded);

                PluginInfo pi = new PluginInfo(p.Instance.PluginInfo);

                e.Content = pi;

                this.cPlugins.Children.Add(e);
            }
        }

        private void Expanded(object sender, RoutedEventArgs e) {
            foreach (Expander expander in this.cPlugins.Children) {
                if (!expander.Equals(sender)) {
                    expander.IsExpanded = false;
                }
            }
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
